/**
 * Created by ltx on 2017/10/6.
 */

function getbloglist() {
    $.ajax({
        url:"/css",
        type:'GET',
        dataType:'JSON',
        success:function (text) {

            //如果请求成功
            headblog(text);
            console.log(text);
        },
        error:function () {
            alert('服务器出错了！！停电了？');
        }
    });
}
/*
 *html的博文列表
 **/
function headblog(text) {
    //console.log(text.length);
    var oMainLeft=document.getElementsByClassName('mainLeft')[0];
    for (var i=0;i<text.length;i++){
        var obowenA=document.createElement("div");
        obowenA.setAttribute("class","bowen");
        oMainLeft.appendChild(obowenA);

        var obowenImg=document.createElement('div');
        obowenImg.setAttribute('class','bowenImg');
        obowenA.appendChild(obowenImg);

        var oImg=document.createElement('img');
        oImg.setAttribute('class','bTu');
        obowenImg.appendChild(oImg);
        var bpic=text[i].src;
        //console.log(bpic);
        $(".bTu").eq(i).attr("src",'/upload/'+bpic);

        var oSpan=document.createElement('span');
        oSpan.setAttribute('class','title');
        obowenA.appendChild(oSpan);
        var bt=text[i].title;
        // console.log(bt);
        $('.title').eq(i).html(bt);

        (function () {
            var z=i;
            document.getElementsByClassName("title")[z].onclick=function () {

                $.ajax({
                    type: 'POST',
                    url: "/user/seenum",
                    data: {
                        article_id:text[z].ID
                    },
                    success: function () {
                        console.log('成功');
                    }
                });
                window.location.href="blogdetails.html?id="+text[z].ID;
            }
        })()

        var oP=document.createElement('p');
        oP.setAttribute('class','shortword');
        obowenA.appendChild(oP);
        var bs=text[i].description;
        //console.log(bs);
        $('.shortword').eq(i).html(bs);

        var obowenTools=document.createElement('div');
        obowenTools.setAttribute('class','tools');
        obowenA.appendChild(obowenTools);

        var toolspan1=document.createElement('span');
        var toolspan1i=document.createElement('i');
        toolspan1.setAttribute('class','see icon-users');
        toolspan1.appendChild(toolspan1i);
        obowenTools.appendChild(toolspan1);
        $('.see').eq(i).html(text[i].custompeople);

        var toolspan2=document.createElement('span');
        var toolspan2i=document.createElement('i');
        toolspan2.setAttribute('class','like icon-happy2');
        toolspan2.appendChild(toolspan2i);
        obowenTools.appendChild(toolspan2);
        //console.log(text[i].nlike);
        $('.like').eq(i).html(text[i].nlike);

        var toolspan3=document.createElement('span');
        var toolspan3i=document.createElement('i');
        toolspan3.setAttribute('class','time icon-table');
        toolspan3.appendChild(toolspan3i);
        obowenTools.appendChild(toolspan3);
        var btime=text[i].post_time.split('T');
        $('.time').eq(i).html(btime[0]);
    }
}

/*统一处理登录注册*/
$(function() {


    getbloglist();
    //登录注册的显示隐藏及发送数据
    var $loginBox = $('#loginBox');
    var $registerBox = $('#registerBox');
    var $userInfo = $('#userInfo');


    //切换到注册面板
    $loginBox.find('a.colMint').on('click', function() {
        $registerBox.show();
        $loginBox.hide();
    });

    //切换到登录面板
    $registerBox.find('a.colMint').on('click', function() {
        $loginBox.show();
        $registerBox.hide();
    });

    //注册
    $registerBox.find('button').on('click', function() {
        //通过ajax提交请求
        $.ajax({
            type: 'post',
            url: '/user/register',
            data: {
                username: $registerBox.find('[name="username"]').val(),
                password: $registerBox.find('[name="password"]').val(),
                repassword:  $registerBox.find('[name="repassword"]').val()
            },
            dataType: 'json',
            success: function(result) {
                $registerBox.find('.colWarning').html(result.message);

                if (!result.code) {
                    //注册成功
                    setTimeout(function() {
                        $loginBox.show();
                        $registerBox.hide();
                    }, 1000);
                }
            }
        });
    });

    //登录
    $loginBox.find('button').on('click', function() {
        //通过ajax提交请求
        $.ajax({
            type: 'post',
            url: '/user/userlogin',
            data: {
                username: $loginBox.find('[name="username"]').val(),
                password: $loginBox.find('[name="password"]').val()
            },
            dataType: 'json',
            success: function(result) {

                $loginBox.find('.colWarning').html(result.message);

                if (result.code==0) {
                    //登录成功
                    $userInfo.show();
                    $userInfo.find('.title > span').html(result.username);
                    $loginBox.hide();
                    sessionStorage.setItem('username', result.username);
                    //  window.location.reload();
                }
                else
                {
                    sessionStorage.clear();
                }
            }
        })
    })
})

